package ks.managers.plugin;

import java.net.URL;
import java.util.Collection;
import java.util.Hashtable;

/**
 * Manages a set of plugins
 * @author harry moreno
 *
 */
public class PluginManager {
	
	/** Store plugins in memory. */
	protected Hashtable<String, Plugin> knownPlugins = new Hashtable<String, Plugin>();
	
	/** clear away all plugins */
	public void removeAll(){
		knownPlugins = new Hashtable<String, Plugin>();
	}
	
	/** adds given plugin to local list
	 * 
	 * @param string
	 * @param url
	 * @return boolean
	 */
	public boolean add(String pluginName, URL url){
		// associate Plugin with this name.
		Plugin plugin = new Plugin (pluginName, url);
		knownPlugins.put(pluginName, plugin);
		
		return true;
	}
	/** alternate way to initialize a plugin,
	 * The plugin must already exist locally,
	 * function looks locally for url value using plugin name
	 * @param pluginName
	 * @param losses
	 * @param wins
	 * @param date
	 * @param suspended
	 * @return
	 */
	public boolean add(String pluginName, int losses, int wins, String date, boolean suspended){
		// alternate plugin addition
		try { 
			Plugin newPlugin = new Plugin(pluginName, new URL("http://google.com/blah.jpg"));
			newPlugin.setLosses(losses);
			newPlugin.setWins(wins);
			newPlugin.setDate(date);
			newPlugin.suspend();
			knownPlugins.put(pluginName, newPlugin);
		}catch (Exception e) {
			e.printStackTrace();

			return false;
		}
		
		return true;
	}
	/** removes plugin from hashtable */
	public boolean remove(String string){
		knownPlugins.remove(string);
		
		return true;
	}
	/** returns plugin with given name */
	public Plugin getPlugin(String name){
		return knownPlugins.get(name);
	}
	/**
	 * Return a collection of Strings representing known plugins in this manager.
	 */
	public Collection<Plugin> getList() {
		return knownPlugins.values();
	}
}
